﻿$(document).ready(function(){
	var _menus = {"menus":[
						{"menuid":"1","icon":"icon-sys","menuname":"公司概况",
							"menus":[
									{"menuid":"12","menuname":"公司简介","icon":"icon-add","url":"editor.php"},
									{"menuid":"13","menuname":"公司理念","icon":"icon-users","url":"editor.php"},
									{"menuid":"14","menuname":"管理团队","icon":"icon-role","url":"editor.php"},
									{"menuid":"15","menuname":"资质荣誉","icon":"icon-set","url":"editor.php"}
								]
						},{"menuid":"2","icon":"icon-sys","menuname":"新闻信息",
							"menus":[{"menuid":"21","menuname":"新增新闻","icon":"icon-add","url":"editor.php"},
									{"menuid":"22","menuname":"新闻列表","icon":"icon-nav","url":"editor.php"}
								]
						},{"menuid":"3","icon":"icon-sys","menuname":"专业服务",
							"menus":[{"menuid":"31","menuname":"新增服务","icon":"icon-add","url":"editor.php"},
									{"menuid":"32","menuname":"服务列表","icon":"icon-nav","url":"editor.php"}
								]
						},{"menuid":"4","icon":"icon-sys","menuname":"项目介绍",
							"menus":[{"menuid":"41","menuname":"新增项目","icon":"icon-add","url":"editor.php"},
									{"menuid":"42","menuname":"项目列表","icon":"icon-nav","url":"editor.php"}
								]
						},{"menuid":"5","icon":"icon-sys","menuname":"合作伙伴",
							"menus":[{"menuid":"41","menuname":"基本资料","icon":"icon-nav","url":"editor.php"},
									{"menuid":"42","menuname":"信息披露","icon":"icon-nav","url":"editor.php"},
									{"menuid":"43","menuname":"公司治理","icon":"icon-nav","url":"editor.php"},
									{"menuid":"44","menuname":"投资者交流","icon":"icon-nav","url":"editor.php"}
								]
						},{"menuid":"6","icon":"icon-sys","menuname":"其余",
							"menus":[{"menuid":"61","menuname":"联系我们","icon":"icon-nav","url":"editor.php"},
									{"menuid":"62","menuname":"网站地图","icon":"icon-nav","url":"editor.php"},
									{"menuid":"63","menuname":"法律声明","icon":"icon-nav","url":"editor.php"},
									{"menuid":"64","menuname":"友情链接","icon":"icon-nav","url":"editor.php"}
								]
						}
				]};
	function InitLeftMenu() {
		$("#west-menu").accordion({animate:true,fit:true});
		$.each(_menus.menus, function(i, n) {
			var menulist ='';
			menulist +='<ul>';
			$.each(n.menus, function(j, o) {
				menulist += '<li><div><a ref="'+o.menuid+'" href="#" rel="' + o.url + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div></li> ';
			})
			menulist += '</ul>';

			$('#west-menu').accordion('add', {
				title: n.menuname,
				content: menulist,
				iconCls: 'icon ' + n.icon
			});

		});

		$('.easyui-accordion li a').click(function(){
			var tabTitle = $(this).children('.nav').text();

			var url = $(this).attr("rel");
			var menuid = $(this).attr("ref");
			var icon = getIcon(menuid);

			addTab(tabTitle,url,icon);
			$('.easyui-accordion li div').removeClass("selected");
			$(this).parent().addClass("selected");
		}).hover(function(){
			$(this).parent().addClass("hover");
		},function(){
			$(this).parent().removeClass("hover");
		});

		//选中第一个
		var panels = $('#west-menu').accordion('panels');
		var t = panels[0].panel('options').title;
		$('#west-menu').accordion('select', t);
	}
	
	function addTab(subtitle,url,icon){
		if(!$('#tabs').tabs('exists',subtitle)){
			$('#tabs').tabs('add',{
				title:subtitle,
				content:createFrame(url),
				closable:true,
				icon:icon
			});
		}else{
			$('#tabs').tabs('select',subtitle);
			$('#mm-tabupdate').click();
		}
		tabClose();
	}
	
	function createFrame(url)
	{
		var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
		return s;
	}
	
	function tabClose()
	{
		/*双击关闭TAB选项卡*/
		$(".tabs-inner").dblclick(function(){
			var subtitle = $(this).children(".tabs-closable").text();
			$('#tabs').tabs('close',subtitle);
		})
		/*为选项卡绑定右键*/
		$(".tabs-inner").bind('contextmenu',function(e){
			$('#mm').menu('show', {
				left: e.pageX,
				top: e.pageY
			});

			var subtitle =$(this).children(".tabs-closable").text();

			$('#mm').data("currtab",subtitle);
			$('#tabs').tabs('select',subtitle);
			return false;
		});
	}
	
	//绑定右键菜单事件
	function tabCloseEven()
	{
		//刷新
		$('#mm-tabupdate').click(function(){
			var currTab = $('#tabs').tabs('getSelected');
			var url = $(currTab.panel('options').content).attr('src');
			$('#tabs').tabs('update',{
				tab:currTab,
				options:{
					content:createFrame(url)
				}
			})
		})
		//关闭当前
		$('#mm-tabclose').click(function(){
			var currtab_title = $('#mm').data("currtab");
			$('#tabs').tabs('close',currtab_title);
		})
		//全部关闭
		$('#mm-tabcloseall').click(function(){
			$('.tabs-inner span').each(function(i,n){
				var t = $(n).text();
				$('#tabs').tabs('close',t);
			});
		});
		//关闭除当前之外的TAB
		$('#mm-tabcloseother').click(function(){
			$('#mm-tabcloseright').click();
			$('#mm-tabcloseleft').click();
		});
		//关闭当前右侧的TAB
		$('#mm-tabcloseright').click(function(){
			var nextall = $('.tabs-selected').nextAll();
			if(nextall.length==0){
				//msgShow('系统提示','后边没有啦~~','error');
				alert('后边没有啦~~');
				return false;
			}
			nextall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				$('#tabs').tabs('close',t);
			});
			return false;
		});
		//关闭当前左侧的TAB
		$('#mm-tabcloseleft').click(function(){
			var prevall = $('.tabs-selected').prevAll();
			if(prevall.length==0){
				alert('到头了，前边没有啦~~');
				return false;
			}
			prevall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				$('#tabs').tabs('close',t);
			});
			return false;
		});

		//退出
		$("#mm-exit").click(function(){
			$('#mm').menu('hide');
		})
	}

	//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
	function msgShow(title, msgString, msgType) {
		$.messager.alert(title, msgString, msgType);
	}
	
	//获取左侧导航的图标
	function getIcon(menuid){
		console.debug(menuid);
		var icon = 'icon ';
		$.each(_menus.menus, function(i, n) {
			 $.each(n.menus, function(j, o) {
				if(o.menuid==menuid){
					icon += o.icon;
				}
			 })
		})

		return icon;
	}
	
	
	InitLeftMenu();
	tabClose();
	tabCloseEven();
	$('#editpass-win').window('close');
	$('#editpass').click(function() {
		$('#editpass-win').window({resizable:false,shadow:true});
	});
	
});
